<template>
	<view>
		<view class="wrap">
			<view class="u-tabs-box">
				<u-tabs :activeStyle="{color: '#303133',fontWeight: 'bold',transform: 'scale(1.05)'}"
					:inactiveStyle="{color: '#606266',transform: 'scale(1)'}" lineColor="#ac4040" ref="tabs"
					:list="list" :current="swiperCurrent" @change="change" :is-scroll="false" swiperWidth="750"></u-tabs>
			</view>

			<u-empty v-if="orderList==0" mode="order" icon="http://cdn.uviewui.com/uview/empty/data.png" />

			<view class="page-box">
				<view class="order" v-for="(item, index) in orderList " :key="item.id">
					<view class="top">
						<view class="left">
							<u-icon name="home" :size="28" color="rgb(94,94,94)"></u-icon>
							<view class="store">{{ item.shopName }}</view>
							<u-icon name="arrow-right" color="rgb(203,203,203)" :size="20"></u-icon>
						</view>
						<view class="right" style="display: flex;justify-content: center;align-items: center;"
							v-if="item.status==1">
							<text style="margin-right: 20rpx;">订单超时取消</text>
							<uni-countdown @timeup="timeup(item)" :showDay="false" :minute="item.between" :second="0" color="#FFFFFF"
								background-color="#ac4040" />
								<!-- item.between -->
						</view>
						<view class="right" v-if="item.status==3">已完成</view>
						<view class="right" v-if="item.status==2">待使用</view>
						<view class="right" v-if="item.status==5">已失效</view>
						<view class="right" v-if="item.status==4">订单已取消</view>

					</view>
					<view class="item"  @click="goAct(item)">
						<view class="left">
							<image :src="item.mainPic" mode="aspectFill"></image>
						</view>
						<view class="content">
							<view class="title u-line-2">{{ item.actName }}</view>
							<view class="type">{{ item.classify }}</view>
							<view class="delivery-time">预约时间 {{ item.appointTime }}</view>
						</view>
						<view class="right">
							<view class="price">
								￥{{ item.price }}
							</view>
							<view class="number">x{{ item.orderNum ? item.orderNum : '1' }}</view>
						</view>
					</view>
					<view class="total" v-if="item.needPay && item.needPay==1">
						共{{ item.orderNum ? item.orderNum : '1' }}件商品 合计:
						<text class="total-price">
							￥{{ item.price}}
						</text>
					</view>
					<view class="bottom">
						<!-- <view class="more">
							<u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon>
						</view> -->
						<view v-if="item.status==2" @click="canleOrder(item.id)" class="logistics btn">取消订单</view>
						<view class="exchange btn" v-if="item.status!=2" @click="edit(item)">{{item.status==2?'修改订单':'删除订单'}}</view>
						<view class="evaluate btn" @click="goPay(item,item.status)" >{{item.status==1?'去支付':'再次预约'}}</view>
					</view>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import utils from '@/common/utils.js'
	export default {
		data() {
			return {
				orderList: [],
				list: [{name: '全部'},{name: '待付款'},{name: '待使用'},{name: '已使用'},{name: '退款/售后'}],
				swiperCurrent: 0,
				tabsHeight: 0,
				dx: 0
			};
		},
		onLoad(e) {
			if(e.idx){
				this.loadTab(e.idx)
				
			}else{
				this.getOrderList(0);
			}
			
		},
		computed: {

		},
		methods: {
			loadTab(idx){
				this.orderList = [];
				this.swiperCurrent = idx;
				uni.showLoading({title: '加载中...'});
				this.getOrderList(idx);
				uni.hideLoading()
			},
			edit(v){
				if(v.status==2){
					//修改订单
				}else{
					uni.showLoading({title: '加载中...'});
					this.$Request.get(this.$api.order.removeOrder, {id:v.id}).then(res => {
						if (res.code === 20000) {
							uni.showToast({
								title: "操作成功!"
							})
							this.orderList=[];
							this.getOrderList(this.swiperCurrent);
							uni.hideLoading();
						}
					})
				}
			},
			timeup(e){
				this.$Request.get(this.$api.order.closeOrder, {id:e.id}).then(res => {
					if (res.code === 20000) {
						this.orderList=[];
						this.getOrderList(this.swiperCurrent);
					}
				})
				
			},
			canleOrder(v){
				uni.showLoading({title: '加载中...'});
				this.$Request.get(this.$api.order.cancelOrder, {id:v}).then(res => {
					if (res.code === 20000) {
						uni.showToast({
							title: res.message
						})
					}
					
					this.timer = setTimeout(() => {
					   this.orderList=[];
					   uni.hideLoading()
					   this.getOrderList(this.swiperCurrent);
					}, 1000);
					
				})
			},
			// 页面数据
			getOrderList(idx) {
				uni.showLoading({title: '加载中...'});
				//状态1-待付款2-待使用3-已使用4-售后
				const params = {
					pageNo: 1,
					pageSize: 10,
					status: idx
				}
				this.$Request.post(this.$api.order.getOrderList, params).then(res => {
					console.log('res', res)
					if (res.code === 20000 && res.data != null) {
						this.orderList = [...this.orderList, ...res.data.records];
						uni.hideLoading()
					}
				})
			},
			// tab栏切换
			change(index) {
				console.log('idx',index)
				this.orderList = [];
				this.swiperCurrent = index.index;
				uni.showLoading({title: '加载中...'});
				this.getOrderList(index.index);
				uni.hideLoading()
			},
			goPay(v,s){
				if(s===1){
					this.$Request.get(this.$api.order.payOrder, {orderSn:v.orderSn}).then(res => {
						console.log('res', res)
						if (res.code === 20000 && res.data != null) {
							uni.requestPayment({
								...res.data,
								success: (res) => {
									uni.showToast({
										title: "支付成功!"
									})
									uni.reLaunch({
										url: '/pages/index/index',
									});
								},
								fail: (res) => {
									uni.showModal({
										content: "支付已取消",
										showCancel: false
									})
									uni.reLaunch({
										url: '/pages/my/index',
									});
								},
								complete: () => {
								}
							})
						}
					})
				}else{
					console.log('v.actId',v.actId)
					utils.removeCache("actInfo");
					uni.reLaunch({
						url: '/pages/activity/components/detail?id='+v.actId,
					});
				}
				
				
				
			},
			goAct(v){
				utils.removeCache("actInfo");
				uni.reLaunch({
					url: '/pages/activity/components/detail?id='+v.actId,
				});
			}
			
		}
	};
</script>

<style>
	/* #ifndef H5 */
	page {
		height: 100%;
		background-color: #f2f2f2;
	}

	/* #endif */
</style>

<style lang="scss" scoped>
	.u-tabs-box {
		margin-top: -5rpx;
		background: rgb(255, 255, 255);
	}

	$nav-height: 30px;

	/* #ifndef APP-NVUE */
	page {
		height: 120%;
	}

	/* #endif */
	.uni-nav-bar-text {
		font-size: 12px;
	}

	.city {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		// width: 160rpx;
		margin-left: 4px;
	}

	.input-view {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		// width: 500rpx;
		flex: 1;
		background-color: #f8f8f8;
		height: $nav-height;
		border-radius: 15px;
		//padding: 0 15px;
		flex-wrap: nowrap;
		margin: 7px 0;
		line-height: $nav-height;
		width: 540rpx;
	}

	.input-uni-icon {
		line-height: $nav-height;
	}

	.nav-bar-input {
		height: $nav-height;
		line-height: $nav-height;
		/* #ifdef APP-PLUS-NVUE */
		width: 370rpx;
		/* #endif */
		padding: 0 5px;
		font-size: 14px;
		background-color: #f8f8f8;
	}

	.example-body {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		padding: 0;
	}

	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 28rpx;
					font-weight: bold;
					width: 150rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}
			}

			.right {
				color: $xdl-color-primary;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: #909399;
				}

				.delivery-time {
					color: $xdl-color-primary;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 20rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: #909399;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: flex-end;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid #e4e7ed;
				font-size: 26rpx;
				text-align: center;
				color: #82848a;
				margin-left: 20rpx;
			}

			.evaluate {
				color: $xdl-color-primary;
				border-color: $xdl-color-primary;
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
			margin: 40rpx auto;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
</style>
